package com.rpay.dao.user;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.rpay.common.util.DateUtil;
import com.rpay.model.user.UserIncomeDO;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/5/13
 * @version 1.0
 */
@Repository
public class UserIncomeDao {
    @Autowired
    @Qualifier("writeSqlSession")
    private SqlSessionTemplate writeSqlSession;

    @Autowired
    @Qualifier("readSqlSession")
    private SqlSessionTemplate readSqlSession;

    public UserIncomeDO selectByUserId(Integer userId, String date) {
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("date", date);
        return readSqlSession.selectOne("rpay.user.income.selectByUserId", map);
    }

    public BigDecimal selectInvestCount(String date) {
        return readSqlSession.selectOne("rpay.user.income.selectInvestCount", date);
    }

    public int updateUserIncome(UserIncomeDO userIncomeDO) {
        return writeSqlSession.update("rpay.user.income.updateUserIncome", userIncomeDO);
    }

    public int insertUserIncome(UserIncomeDO userIncomeDO) {
        return writeSqlSession.insert("rpay.user.income.insertUserIncome", userIncomeDO);
    }

    public List<UserIncomeDO> selectAll(Integer userId) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("date", DateUtil.formatDate(new Date()));
        return readSqlSession.selectList("rpay.user.income.selectAll", map);
    }

    public int updateAll(List<UserIncomeDO> list) {
        return writeSqlSession.update("rpay.user.income.updateAll", list);
    }

    public int insertAll(List<UserIncomeDO> list) {
        return writeSqlSession.insert("rpay.user.income.insertAll", list);
    }

    public UserIncomeDO selectByStatistics(Integer userId, String date) {
        Map map = Maps.newHashMap();
        map.put("userId",userId);
        map.put("date", date);
        return readSqlSession.selectOne("rpay.user.income.selectByStatistics", map);
    }

    public int selectCount(Integer userId, String beginDate, String endDate) {
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return readSqlSession.selectOne("rpay.user.income.selectCount", map);
    }

    public List<UserIncomeDO> selectUserIncomes(Integer userId, String beginDate, String endDate, Integer pageOffset, Integer pageSize) {
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        map.put("pageOffset", pageOffset);
        map.put("pageSize", pageSize);
        return readSqlSession.selectList("rpay.user.income.selectUserIncomes", map);
    }
}
